home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / SQLRUN.CAB / sp1Tools80.sql.B1EA9824_A6BB_4729_9176_90F7D8D6EC7A < prev    next >
Encoding:
Text File  |  2001-04-13  |  70.8 KB  |  1,665 lines

  1. /*------------------------------------------------------------------------------
  2.  
  3. 80SP1_TOOLS.SQL
  4.  
  5. THIS SCRIPT TAKES THE TOOLS STORED PROCS FROM 8.0 to SP1.
  6.  
  7. Changes in this file reflects changes in the following files:
  8.     INSTMSDB.SQL
  9.     SQLDMO.SQL
  10.     XPSTAR.SQL
  11.     SQLTRACE.SQL
  12.     WEB.SQL
  13.  
  14. Notes:
  15.  
  16. ------------------------------------------------------------------------------*/
  17.  
  18. PRINT N''
  19. PRINT N'Updating database objects, executing 80SP1-TOOLS.SQL'
  20. PRINT N'Started at ' + convert(nvarchar(25), getdate())
  21. PRINT N''
  22. go
  23.  
  24. --------------------------------------------------------------------------------
  25. -- VERIFY Server is started in single-user-mode (catalog-updates enabled), 
  26. -- and start marking of system-objects.
  27. --------------------------------------------------------------------------------
  28. use master
  29. go
  30.  
  31. exec dbo.sp_configure 'allow updates',1
  32. go
  33.  
  34. reconfigure with override
  35. go
  36.  
  37. exec sp_MS_upd_sysobj_category 1
  38. go
  39.  
  40.  
  41. --------------------------------------------------------------------------------
  42. -- SQLDMO stored procedures added after release into sqldmo.sql file
  43. --
  44. -- sp_MSobjectprivs
  45. --------------------------------------------------------------------------------
  46.  
  47. /**************************************************************/
  48. /* sp_MSobjectprivs                                           */
  49. /**************************************************************/
  50. if exists (select * from master..sysobjects where (OBJECTPROPERTY(id, N'IsProcedure') = 1 or OBJECTPROPERTY(id, N'IsExtendedProc') = 1) and name = N'sp_MSobjectprivs')
  51.     drop procedure dbo.sp_MSobjectprivs
  52. go
  53.  
  54. print N''
  55. print N'Creating procedure sp_MSobjectprivs...'
  56. go
  57.  
  58. create proc dbo.sp_MSobjectprivs
  59.     @objname nvarchar(776) = null,
  60.     @mode nvarchar(10) = N'object',    
  61.     @objid int = null,                
  62.     @srvpriv int = null,            
  63.     @prottype int = null,            
  64.     @grantee nvarchar(258) = null,        
  65.     @flags int = 0,
  66.     @rollup int = 0
  67. as
  68.     create table #objs(id  int NOT NULL)
  69.  
  70.     /* Temp table will hold output for final select */
  71.     create table #output (
  72.         action      int  NOT NULL,
  73.         colid       int  NULL,
  74.         uid         int  NOT NULL,
  75.         protecttype int  NOT NULL,
  76.         id          int  NOT NULL,
  77.         grantor     int
  78.     )
  79.  
  80.     create table #tmp(
  81.         action   int   NOT NULL,
  82.         uid      int   NOT NULL,
  83.         protecttype int  NOT NULL,
  84.     )
  85.  
  86.    /* mode    : 'object', 'user' or 'column'*/
  87.    /*
  88.     * Note:  This was expanded for 6.5 due to changes in sysprotects.columns usage, affecting
  89.     * CPermission::ListPrivilegeColumns.  The following additional parameters are for this.
  90.     */
  91.    /* objid   : ID of the object we're querying */
  92.    /* srvpriv : privilege that we're querying for (e.g. select) */
  93.    /* prottype: Protect type, e.g. GRANT/REVOKE */
  94.    /* grantee : Grantee name. */
  95.  
  96.    /*** @flags added for DaVinci uses.  If the bit isn't set, use 6.5 ***/
  97.    /*** sp_MSobjectprivs '%s'                                         ***/
  98.  
  99.    /* 8.0: mode 'column', and grantee != null, we want user column level permissions for CTable/CView::ListUserColumnPermissions */
  100.    /*      @rollup added to indicate special rollup result set for column level permission, set to 1 to roll up */
  101.  
  102.     /* @flags is for daVinci */
  103.     if (@flags is null)
  104.         select @flags = 0
  105.  
  106.     /* If @objid is not null, this is for the new query for perm cols. */
  107.     if (@objid is not null) begin
  108.         select u.name, o.name, a = col_name(p.id, a.number), a.low, a.high, a.number
  109.             from master.dbo.spt_values a, dbo.sysprotects p, dbo.sysobjects o, dbo.sysusers u
  110.             where p.id = @objid and p.action = @srvpriv and p.protecttype = @prottype
  111.             and p.uid = user_id(@grantee)
  112.             and p.columns != 0x01 and o.id = p.id and u.uid = o.uid
  113.                 and convert(tinyint, substring(isnull(p.columns, 0x01), a.low, 1)) &
  114.                     -- 6.5 changed so that the bit 0 position is an "invert the bits" indicator:
  115.                     --        when 0, behaviour is the same as in prior versions, and other bits
  116.                     --            indicate columns with the specified privilege
  117.                     --        when 1, the other bits are indicate columns lacking the specified privilege
  118.                     a.high <> (case when (substring(isnull(p.columns, 0x00), 1, 1) & 1 = 0) then 0 else a.high end)
  119.                     and col_name(p.id, a.number) is not null
  120.                     and a.type = N'P' and a.number <= (select count(*) from dbo.syscolumns where id = @objid) order by a
  121.         return 0
  122.     end
  123.  
  124.     set nocount on
  125.  
  126.     /*
  127.      * To get around a 4.21 subquery bug where returning count(*) of 0 (for proc cols)
  128.      * causes the result set to return no rows, we need two passes; one to get the
  129.      * objects, and another to explicitly use a value (@cols) instead of a subquery.
  130.      */
  131.     declare @id int, @uid int, @cols int
  132.     select @id = null, @uid = null
  133.     if (@mode like N'us%') begin
  134.        select @uid = user_id(@objname)
  135.    end else if (@mode like N'col%') and (@objname is null) and (@grantee is not null) begin
  136.       /* 8.0, special path to get column level permissions from all objects on the specified user */
  137.       select @uid = user_id(@grantee)
  138.     end else begin
  139.       select @id = object_id(@objname)
  140.    end
  141.     if (@id is null and @uid is null) begin
  142.         RAISERROR (15001, -1, -1, @objname)
  143.         return 1
  144.     end
  145.  
  146.     /* Get a temp list of objects we're interested in.  Do not include repl_* users. */
  147.    /* This is the original code */
  148.    insert #objs select distinct p.id from dbo.sysprotects p
  149.        where (@id is null or p.id = @id)
  150.           and (@uid is null or p.uid = @uid)
  151.        and p.action in (193, 195, 196, 197, 224, 26) and p.uid not in (16382, 16383)
  152.  
  153.     /* Use a "fake cursor" by deleting successive id's from #objs, as this must run on 4.21 */
  154.     select @id = min(id) from #objs
  155.     while (@id is not null) begin
  156.         select @cols = count(*) from dbo.syscolumns c where c.id = @id
  157.       /* sysprotects.columns is for SELECT and UPDATE, NULL if it is INSERT or DELETE, since INSERT and DELETE can not be applied to column level */
  158.       insert #output select p.action, (case when p.columns is null then -1 else a.number end), p.uid, p.protecttype, p.id, p.grantor
  159.          from master.dbo.spt_values a, dbo.sysprotects p
  160.          where convert(tinyint, substring( isnull(p.columns, 0x01), a.low, 1)) & a.high !=0
  161.          and (p.id = @id)
  162.          and (@uid is null or p.uid = @uid)
  163.          and a.number <= @cols
  164.          and a.type = N'P'
  165.  
  166.       declare @count int, @whataction int, @whatid int, @dup int, @whatprot int
  167.  
  168.       /* First pass to correct duplicates */
  169.       select @count = count(*) from #output where id = @id and colid in (0, -1) and protecttype in (205, 204)
  170.       if ( @count > 0 ) begin
  171.          /* We might have duplicate rows for permission on single coulmn(s) at this point */
  172.          /* Use a fake cursor to remove the duplicates first. */
  173.          insert #tmp select action, uid, protecttype from #output where id = @id and colid in (0, -1) and protecttype in (205, 204)
  174.          select @whataction = min(action) from #tmp
  175.          select @whatid = uid from #tmp where action = @whataction
  176.          while (@whataction is not null) begin
  177.             if (@mode like N'col%') and (@objname is null) and (@grantee is not null) begin
  178.                /* Special case for column level permissions on ALL objects for the specified user, we don't want the row(s) on the entire table */
  179.                /* and we don't want the possible duplicate rows in single column(s) */
  180.                delete #output where (@whatid = uid) and (colid not in (0, -1)) and (protecttype in (205, 204)) and action = @whataction
  181.                       and (exists (select * from #output where (@whatid = uid) and (colid in (0, -1)) and action = @whataction) and (id = @id))
  182.                delete #output where (@whatid = uid) and (colid in (0, -1)) and (action = @whataction) and (id = @id)
  183.             end else if (@mode like N'use%') and (@objname is not null) begin
  184.                /* Special case for the user mode, we do want to keep the entire table permissions */
  185.                delete #output where (@whatid = uid) and (colid not in (0, -1)) and (protecttype in (205, 204)) and action = @whataction and (id = @id)
  186.             end else begin
  187.                /* Other cases */
  188.                delete #output where (@whatid = uid) and (colid not in (0, -1)) and (protecttype in (205, 204)) and action = @whataction
  189.             end
  190.  
  191.             delete #tmp where @whatid = uid
  192.             select @whataction = min(action) from #tmp
  193.             select @whatid = uid from #tmp where action = @whataction
  194.          end
  195.          delete #tmp
  196.       end
  197.  
  198.       /* Second pass to correct protect type */
  199.       select @count = count(*) from #output where id = @id and colid in (0, -1)
  200.       if ( @count > 0 ) begin
  201.          /* use another fake cursor to correct the protecttype */
  202.          /* if there are multiple rows in #output for the same id and action, and if colid = 0 exist */
  203.          /* then other rows should have different protecttype from the one in colid = 0 row */
  204.          insert #tmp select action, uid, protecttype from #output where id = @id and colid in (0, -1)
  205.          select @whataction = min(action) from #tmp
  206.          select @whatid = uid from #tmp where action = @whataction
  207.          select @whatprot = protecttype from #tmp where uid = @whatid and action = @whataction
  208.          while (@whataction is not null) begin
  209.                delete #output where id = @id and colid not in (0, -1) and @whataction = action and @whatid = uid and @whatprot = protecttype
  210.                delete #tmp where action = @whataction and @whatid = uid
  211.                select @whataction = min(action) from #tmp
  212.                select @whatid = uid from #tmp where action = @whataction
  213.                select @whatprot = protecttype from #tmp where uid = @whatid and action = @whataction
  214.          end
  215.          delete #tmp
  216.       end
  217.  
  218.         /* Increment our "fake cursor" column and get the next one. */
  219.         delete #objs where id = @id
  220.         select @id = min(id) from #objs
  221.     end
  222.  
  223.     /*
  224.      * Organize so that the non-collist privileges are returned first.. this allows
  225.      * scripting to combine them.  sysprotects.action is tinyint, so the hibyte won't conflict.
  226.      */
  227.  
  228.     update #output set action = action | 0x10000000 where colid <> 0
  229.  
  230.     /*
  231.      *  BUG 58252  
  232.      *  Delete the columns that was droped
  233.      */
  234.     delete from #output where colid not in (0, -1) and col_name(id, colid) is null
  235.  
  236.  
  237.     /*
  238.      * Order output by uid so Public will script before other groups (we need to script privs for public before
  239.      * other groups, before users; otherwise sysprotects doesn't hold onto things right).  Sub-order is by object id
  240.      * so we know when we're done with one object and onto the next, then by protecttype to group all GRANTs and
  241.      * REVOKEs together, and lastly by action (including ORDER_ACTION_BIT so scripting can be more efficient)
  242.      * because we may have multiple rows for columns.
  243.      */
  244.  
  245.     set nocount off
  246.    if (@mode not like N'col%') begin
  247.       /* Mode is not 'column', do the regular stuff */
  248.        select p.action & ~convert(int, 0x10000000), N'column' = col_name(p.id, p.colid), p.uid, N'username' = user_name(p.uid),
  249.                p.protecttype, o.name, N'owner' = user_name(o.uid), p.id, N'grantor' = user_name(p.grantor)
  250.              from #output p, dbo.sysobjects o
  251.              where o.id = p.id
  252.              order by p.uid, p.id, p.protecttype, p.action
  253.    end else
  254.    /* Below are spcial cases for column level permissions */
  255.    if (@objname is null) and (@grantee is not null) and (@rollup = 0) begin
  256.       /* 8.0, special path to get column level permissions from all objects on the specified user */
  257.       select N'ObjectName' = o.name, N'Owner' = user_name(o.uid), N'ColumnName' = col_name(p.id, p.colid), o.sysstat & 0x0f, p.id,
  258.              p.action & ~convert(int, 0x10000000), p.protecttype
  259.              from #output p, dbo.sysobjects o
  260.              where p.id = o.id and p.uid = user_id(@grantee) and col_name(p.id, p.colid) is not null
  261.              order by p.uid, p.id, p.protecttype, p.action
  262.     end else if (@objname is not null) and (@grantee is not null) and (@rollup = 0) begin
  263.       /* 8.0, mode 'column', and grantee != null, we want column level permissions on this object for this user */
  264.       select N'column' = col_name(p.id, p.colid), N'owner' = user_name(o.uid), N'username' = user_name(p.uid), o.sysstat & 0x0f, p.id,
  265.              p.action & ~convert(int, 0x10000000), p.protecttype
  266.              from #output p, dbo.sysobjects o
  267.              where o.id = p.id and p.uid = user_id(@grantee) and col_name(p.id, p.colid) is not null
  268.              order by p.uid, p.id, p.protecttype, p.action
  269.    end else if (@objname is not null) and (@grantee is null) and (@rollup = 0) begin
  270.       /* 8.0, mode 'column', and grantee = null, we want column level permissions on this object for all users */
  271.       select N'column' = col_name(p.id, p.colid), N'owner' = user_name(o.uid), N'username' = user_name(p.uid), o.sysstat & 0x0f, p.id,
  272.              p.action & ~convert(int, 0x10000000), p.protecttype
  273.              from #output p, dbo.sysobjects o
  274.              where o.id = p.id and col_name(p.id, p.colid) is not null
  275.              order by p.uid, p.id, p.protecttype, p.action
  276.    end else if (@objname is null) and (@grantee is not null) and (@rollup <> 0) begin
  277.       /* 8.0, roll up version of the special path to get column level permissions from all objects on the specified user */
  278.       select distinct N'ObjectName' = o.name, N'owner' = user_name(o.uid),
  279.              N'Select' = (case when ((p.action & ~convert(int, 0x10000000))=193) then 1 else 0 end),
  280.              N'Update' = (case when ((p.action & ~convert(int, 0x10000000))=197) then 1 else 0 end),
  281.              N'Type' = p.protecttype
  282.              from #output p, dbo.sysobjects o
  283.              where p.id = o.id and p.uid = user_id(@grantee) and col_name(p.id, p.colid) is not null
  284.              order by o.name
  285.    end else if (@objname is not null) and (@grantee is null) and (@rollup <> 0) begin
  286.       /* 8.0, roll up version of the special path to return column level permissions on this object for all users */
  287.       select distinct N'UserName' = user_name(p.uid),
  288.              N'Select' = (case when ((p.action & ~convert(int, 0x10000000))=193) then 1 else 0 end),
  289.              N'Update' = (case when ((p.action & ~convert(int, 0x10000000))=197) then 1 else 0 end),
  290.              N'Type' = p.protecttype
  291.              from #output p, dbo.sysobjects o
  292.              where o.id = p.id and col_name(p.id, p.colid) is not null
  293.              order by user_name(p.uid)
  294.    end else begin
  295.       raiserror 55555 N'Invalid parameter combinations.'
  296.         return 1
  297.    end
  298. go
  299. /* End sp_MSobjectprivs */
  300.  
  301.  
  302. exec sp_MS_marksystemobject sp_MSobjectprivs
  303. go
  304.  
  305. grant execute on sp_MSobjectprivs to public
  306.  
  307. --------------------------------------------------------------------------------
  308. -- END SQLDMO SECTION
  309. --------------------------------------------------------------------------------
  310.  
  311. --------------------------------------------------------------------------------
  312. -- Changes made to XPSTAR.sql file for SP1
  313. --
  314. -- xp_adsirequest          
  315. -- sp_ActiveDirectory_Obj  
  316. -- xp_GetAdminGroupName       
  317. -- sp_ActiveDirectory_SCP  
  318. --------------------------------------------------------------------------------
  319.  
  320.  
  321. /**************************************************************/
  322. /* xp_adsirequest                                             */
  323. /**************************************************************/
  324. if exists (select * from sysobjects where name = N'xp_adsirequest' and type = N'X')
  325.     exec sp_dropextendedproc N'xp_adsirequest'
  326. go
  327.  
  328. print N''
  329. print N'Creating extended stored procedure xp_adsirequest...'
  330. go
  331.  
  332. exec sp_addextendedproc N'xp_adsirequest',N'xpstar.dll'
  333. go
  334.  
  335. /**************************************************************/
  336. /* sp_ActiveDirectory_Obj                                     */
  337. /**************************************************************/
  338. if exists (select * from sysobjects where name = N'sp_ActiveDirectory_Obj' and type = N'P')
  339.     drop procedure sp_ActiveDirectory_Obj
  340. go
  341.  
  342. print N''
  343. print N'Creating procedure sp_ActiveDirectory_Obj...'
  344. go
  345.  
  346. create proc dbo.sp_ActiveDirectory_Obj
  347.     @Action          nvarchar(10) = N'create',    -- create, update, delete
  348.     @ObjType         nvarchar(15) = N'database',    -- database, publication
  349.     @ObjName         sysname  = null,        -- object name
  350.     @DatabaseName    sysname = null,         -- database name for publication object
  351.     @GUIDName        sysname = null          -- GUID for publication update and delete
  352. as
  353. begin
  354.    /* create : create the object under the current SCP object. */
  355.    /* update : update the object under the SCP object.         */
  356.    /* delete : delete the object under the SCP object.         */
  357.  
  358.    SET NOCOUNT ON
  359.  
  360.    DECLARE @isdbowner int
  361.    DECLARE @cmd nvarchar(255)
  362.    DECLARE @commonname nvarchar(300)
  363.    DECLARE @retcode int
  364.    DECLARE @nAction nvarchar(3)
  365.    DECLARE @Tmp nvarchar(10)
  366.    DECLARE @dbname sysname
  367.  
  368.    DECLARE @retval int
  369.    DECLARE @SQLADSI_COM_ERROR int
  370.    DECLARE @SQLADSI_UNEXP_ERROR int
  371.    DECLARE @SQLADSI_SCP_NOT_FOUND int
  372.    DECLARE @SQLADSI_SVC_ACCT_ERROR int
  373.    DECLARE @SQLADSI_CANNOT_START_HLP int
  374.    DECLARE @SQLADSI_TIMEOUT_WAIT_HLP int
  375.    DECLARE @SQLADSI_AD_NOT_INSTALLED int
  376.    DECLARE @SQLADSI_PROXY_ACCT_ERROR int
  377.  
  378.    SELECT @SQLADSI_COM_ERROR = 536870913
  379.    SELECT @SQLADSI_UNEXP_ERROR = 536870914
  380.    SELECT @SQLADSI_SCP_NOT_FOUND = 536870915
  381.    SELECT @SQLADSI_SVC_ACCT_ERROR = 536870916
  382.    SELECT @SQLADSI_CANNOT_START_HLP = 536870917
  383.    SELECT @SQLADSI_TIMEOUT_WAIT_HLP = 536870918
  384.    SELECT @SQLADSI_AD_NOT_INSTALLED = 536870919
  385.    SELECT @SQLADSI_PROXY_ACCT_ERROR = 536870920
  386.  
  387.    /* check permissions
  388.    IF (not is_srvrolemember(N'sysadmin') = 1)
  389.    begin
  390.       raiserror(15003,-1,-1, N'sysadmin')
  391.       return 1
  392.    end
  393.    */
  394.  
  395.    /* If publication object, we need both object name and database name */
  396.    if ((UPPER(@ObjType) in (N'PUBLICATION')) and ((@ObjName is null) or (@DatabaseName is null)))
  397.    begin
  398.       raiserror(14200, -1, -1, N'@ObjName or @DatabaseName')
  399.       return 1
  400.    end
  401.  
  402.  
  403.    /* check parameters */
  404.    if (@Action is null OR UPPER(@Action) not in (N'CREATE', N'UPDATE', N'DELETE'))
  405.    begin
  406.       raiserror(14266, -1, -1, N'@Action', N'CREATE, UPDATE, DELETE')
  407.       return 1
  408.    end
  409.    if (@ObjType is null OR UPPER(@ObjType) not in (N'DATABASE', N'REPOSITORY', N'PUBLICATION'))
  410.    begin
  411.       raiserror(14266, -1, -1, N'@ObjType', N'DATABASE, REPOSITORY, PUBLICATION')
  412.       return 1
  413.    end
  414.    if (@ObjName is null)
  415.    begin
  416.       raiserror(14200, -1, -1, N'@ObjName')
  417.       return 1
  418.    end
  419.  
  420.    /* If publication object update or delete, we need GUID also */
  421.    if ((UPPER(@ObjType) in (N'PUBLICATION')) and UPPER(@Action) in (N'UPDATE', N'DELETE') and (@GUIDName is null))
  422.    begin
  423.       raiserror(14200, -1, -1, N'@GUIDNName')
  424.       return 1
  425.    end
  426.  
  427.    if (UPPER(@ObjType) in (N'PUBLICATION'))
  428.       select @dbname = @DatabaseName
  429.    else
  430.       select @dbname = @ObjName
  431.  
  432. -- Make sure the database exists
  433. --
  434.    if not exists (select * from master.dbo.sysdatabases where name = @dbname)
  435.    begin
  436.       raiserror(15010,-1,-1,@dbname)
  437.       return (1)
  438.    end
  439.  
  440.    /* Check permissions.  */
  441.    SELECT @cmd = 'USE ' + quotename(@dbname) + ' SELECT @isdbowner = is_member(''db_owner'')'
  442.  
  443.    EXEC @retcode = sp_executesql @cmd, N'@isdbowner int output', @isdbowner output
  444.    IF @@error <> 0 or @retcode <> 0
  445.       return 1
  446.  
  447.    IF (is_srvrolemember('sysadmin') <> 1 and isnull(@isdbowner, 0) <> 1)
  448.    BEGIN
  449.       raiserror(21050, 14, -1)
  450.       return 1
  451.    END
  452.  
  453.    /* common name length check */ 
  454.    if (UPPER(@ObjType) in (N'PUBLICATION'))
  455.        SELECT @commonname = @ObjName + N':' + @DatabaseName
  456.    else
  457.        SELECT @commonname = @ObjName
  458.   
  459.    IF (LEN(@commonname) > 64)
  460.       RAISERROR(14357, -1, -1, @commonname)
  461.        
  462.    select @Tmp = UPPER(@Action)
  463.    if (UPPER(@Tmp) like N'CRE%')
  464.       select @nAction = N'1'
  465.    else if (UPPER(@Tmp) like N'UPD%')
  466.       select @nAction = N'2'
  467.    else if (UPPER(@Tmp) like N'DEL%')
  468.       select @nAction = N'3'
  469.  
  470.    declare @nObjType nvarchar(3)
  471.    select @Tmp = UPPER(@ObjType)
  472.    if (UPPER(@Tmp) like N'DATAB%')
  473.       select @nObjType = N'2'
  474.    else if (UPPER(@Tmp) like N'REPOS%')
  475.       select @nObjType = N'3'
  476.    else if (UPPER(@Tmp) like N'PUBL%')
  477.       select @nObjType = N'4'
  478.  
  479.    /* are we running on Windows 2000 or NT4 SP5 with AD enabled?  continue only if TRUE */
  480.    EXECUTE @retval = master.dbo.xp_MSADEnabled
  481.    if (@retval = 0)
  482.    begin
  483.       /* prepare parameters */
  484.       declare @InstanceName sysname
  485.       declare @ServerName sysname
  486.       select @InstanceName = convert(sysname, serverproperty(N'InstanceName'))
  487.       select @ServerName = convert(sysname, serverproperty(N'ServerName'))
  488.       if (@InstanceName is NULL)
  489.          select @InstanceName = N'MSSQLSERVER'
  490.  
  491.       /* Need to create registry values only if create or update. */
  492.       if (@nAction <> N'3')
  493.       begin
  494.           EXECUTE @retval = master.dbo.xp_MSADSIObjReg @InstanceName, @nAction, @nObjType, @ObjName, @DatabaseName, @ServerName
  495.       end
  496.       if (@retval = 0)
  497.       begin
  498.          /* call xp with the valid parameters, xp_cmdshell expects double quote begin and end */
  499.          DECLARE @args NVARCHAR(512)
  500.          if ((@nObjType like N'4') and (@nAction like N'1'))
  501.          begin
  502.             /* PUBLICATION creation */
  503.             SELECT @args = @InstanceName + N' ' + @nAction +  N' ' + @nObjType + N' '  + quotename(@ObjName, N'"') + N' ' + quotename(@DatabaseName, N'"') 
  504.          end else if ((@nObjType like N'4') and (@nAction not like N'1'))
  505.          begin
  506.             /* PUBLICATION update or delete */
  507.             SELECT @args = @InstanceName + N' ' + @nAction +  N' ' + @nObjType + N' ' + quotename(@ObjName, N'"') + N' ' + quotename(@DatabaseName, N'"') + N' ' + @GUIDName
  508.          end else
  509.          begin
  510.             /* Non PUBLICATION objects */
  511.             SELECT @args = @InstanceName + N' ' + @nAction +  N' ' + @nObjType + N' ' + quotename(@ObjName, N'"')
  512.          end
  513.  
  514.          EXECUTE @retval = master.dbo.xp_adsirequest @args
  515.          if (@retval = 0)
  516.          begin
  517.             if (@nAction = N'3')
  518.             begin
  519.                 EXECUTE @retval = master.dbo.xp_MSADSIObjReg @InstanceName, @nAction, @nObjType, @ObjName, @DatabaseName, @ServerName
  520.                 if (@retval <> 0)
  521.                 begin
  522.                     raiserror(14303, -1, -1, N'sp_ActiveDirectory_Obj')
  523.                     return 1
  524.                 end
  525.             end
  526.          end
  527.          else
  528.          begin
  529.             if @retval = @SQLADSI_COM_ERROR 
  530.                 RAISERROR(14350, -1, -1)
  531.             else if @retval = @SQLADSI_UNEXP_ERROR 
  532.                 RAISERROR(14351, -1, -1)
  533.             else if @retval = @SQLADSI_SCP_NOT_FOUND 
  534.                 RAISERROR(14352, -1, -1)
  535.             else if @retval = @SQLADSI_SVC_ACCT_ERROR 
  536.                 RAISERROR(14353, -1, -1)
  537.             else if @retval = @SQLADSI_CANNOT_START_HLP 
  538.                 RAISERROR(14354, -1, -1)
  539.             else if @retval = @SQLADSI_TIMEOUT_WAIT_HLP 
  540.                 RAISERROR(14355, -1, -1)
  541.             else if @retval = @SQLADSI_AD_NOT_INSTALLED 
  542.                 RAISERROR(14356, -1, -1)
  543.             else if @retval = @SQLADSI_PROXY_ACCT_ERROR 
  544.                 RAISERROR(14358, -1, -1)
  545.    
  546.             /* Failed */
  547.             return 1
  548.          end
  549.       end else
  550.       begin
  551.          raiserror(14303, -1, -1, N'sp_ActiveDirectory_Obj')
  552.          return 1
  553.       end
  554.    end else
  555.    begin
  556.       raiserror(14304, -1, -1, N'sp_ActiveDirectory_Obj')
  557.       return 1
  558.    end
  559. end
  560. go
  561.  
  562. grant execute on sp_ActiveDirectory_Obj to public
  563. go
  564.  
  565. /**************************************************************/
  566. /* xp_GetAdminGroupName                                       */
  567. /**************************************************************/
  568. if exists (select * from sysobjects where name = N'xp_GetAdminGroupName' and type = N'X')
  569.     exec sp_dropextendedproc N'xp_GetAdminGroupName'
  570. go
  571.  
  572. print N''
  573. print N'Creating extended stored procedure xp_GetAdminGroupName...'
  574. go
  575.  
  576. exec sp_addextendedproc N'xp_GetAdminGroupName',N'xpstar.dll'
  577. go
  578.  
  579. grant execute on xp_GetAdminGroupName to public
  580. go
  581.  
  582. /**************************************************************/
  583. /* sp_ActiveDirectory_SCP                                     */
  584. /**************************************************************/
  585. if exists (select * from sysobjects where name = N'sp_ActiveDirectory_SCP' and type = N'P')
  586.     drop procedure sp_ActiveDirectory_SCP
  587. go
  588.  
  589. print N''
  590. print N'Creating procedure sp_ActiveDirectory_SCP...'
  591. go
  592.  
  593. create proc dbo.sp_ActiveDirectory_SCP
  594.     @Action  nvarchar(20) = N'create',    -- create_noupdate, create_with_db, create, update, delete, shutdown
  595.     @Startup int = 0                      -- 0 for non-startup, non-zero if called from server startup
  596. as
  597. begin
  598.    /* create_noupdate         : create the SCP object, if it exists already, update it.                                     */
  599.    /*                           create the DB objects only if they don't exists yet.  Do not update the existig DB objects. */
  600.    /* create_with_db          : create the SCP object, if it exsits already, update it.                                     */
  601.    /*                           Create all the DB objects under the SCP object.  If a DB object exists already, update it.  */
  602.    /* create (DEFAULT)        : create the SCP object, if it exists already, update it.                                     */
  603.    /* update                  : update the SCP object.                                                                      */
  604.    /* shutdown                : mark the SCP object to indicate not running, but don't delete it.                           */
  605.    /* delete                  : delete the SCP object and all the objects below it.                                         */
  606.  
  607.    SET NOCOUNT ON
  608.  
  609.    DECLARE @retval int
  610.    DECLARE @SQLADSI_COM_ERROR int
  611.    DECLARE @SQLADSI_UNEXP_ERROR int
  612.    DECLARE @SQLADSI_SCP_NOT_FOUND int
  613.    DECLARE @SQLADSI_SVC_ACCT_ERROR int
  614.    DECLARE @SQLADSI_CANNOT_START_HLP int
  615.    DECLARE @SQLADSI_TIMEOUT_WAIT_HLP int
  616.    DECLARE @SQLADSI_AD_NOT_INSTALLED int
  617.    DECLARE @SQLADSI_PROXY_ACCT_ERROR int
  618.  
  619.    SELECT @SQLADSI_COM_ERROR = 536870913
  620.    SELECT @SQLADSI_UNEXP_ERROR = 536870914
  621.    SELECT @SQLADSI_SCP_NOT_FOUND = 536870915
  622.    SELECT @SQLADSI_SVC_ACCT_ERROR = 536870916
  623.    SELECT @SQLADSI_CANNOT_START_HLP = 536870917
  624.    SELECT @SQLADSI_TIMEOUT_WAIT_HLP = 536870918
  625.    SELECT @SQLADSI_AD_NOT_INSTALLED = 536870919
  626.    SELECT @SQLADSI_PROXY_ACCT_ERROR = 536870920
  627.  
  628.    /* check permissions */
  629.    IF (not is_srvrolemember(N'sysadmin') = 1)
  630.    begin
  631.       raiserror(15003,-1,-1, N'sysadmin')
  632.       return 1
  633.    end
  634.  
  635.    /* check parameters */
  636.    if (@Action is null OR UPPER(@Action) not in (N'CREATE', N'UPDATE', N'DELETE', N'SHUTDOWN', N'CREATE_WITH_DB', N'CREATE_NOUPDATE'))
  637.    begin
  638.       raiserror(14266, -1, -1, N'@Action', N'CREATE, UPDATE, DELETE, SHUTDOWN, CREATE_WITH_DB, CREATE_NOUPDATE')
  639.       return 1
  640.    end
  641.  
  642.    declare @nAction nvarchar(3)
  643.    declare @Tmp nvarchar(10)
  644.    select @Tmp = UPPER(@Action)
  645.    if (UPPER(@Tmp) like N'CRE%')
  646.       select @nAction = N'1'
  647.    else if (UPPER(@Tmp) like N'UPD%')
  648.       select @nAction = N'2'
  649.    else if (UPPER(@Tmp) like N'DEL%')
  650.       select @nAction = N'3'
  651.    else if (UPPER(@Tmp) like N'SHU%')
  652.       select @nAction = N'4'
  653.  
  654.    /* are we running on Windows 2000 or NT4 SP5 with AD enabled?  continue only if TRUE */
  655.    EXECUTE @retval = master.dbo.xp_MSADEnabled
  656.    if (@retval = 0)
  657.    begin
  658.       /* Get the correct path for xpadsi.exe */
  659.       declare @Data nvarchar(256)
  660.       exec master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\Setup', N'SQLPath', @param = @Data OUT, @no_output = N'no_output'
  661.  
  662.       declare @BlankIndex int
  663.       select @BlankIndex = charindex(N' ', @Data)
  664.       if (@BlankIndex is NULL)
  665.           select @BlankIndex = 0
  666.  
  667.       /* Gather information */
  668.       declare @InstanceName sysname
  669.       declare @ServerName sysname
  670.       select @InstanceName = convert(sysname, serverproperty(N'InstanceName'))
  671.       select @ServerName = convert(sysname, serverproperty(N'ServerName'))
  672.       if (@InstanceName is NULL)
  673.          select @InstanceName = N'MSSQLSERVER'
  674.  
  675.       /* Need to create registry values only if create or update.  Delete registry when delete */
  676.       if (@nAction <> N'3')
  677.       begin
  678.           EXECUTE @retval = master.dbo.xp_MSADSIReg @InstanceName, @nAction, @ServerName
  679.       end
  680.       if (@retval = 0)
  681.       begin
  682.          /* call xp with the valid parameters */
  683.          DECLARE @command NVARCHAR(512)
  684.          DECLARE @nStartup NVARCHAR(5)
  685.          if (@Startup = 0)
  686.             select @nStartup = N'0'
  687.          else
  688.             select @nStartup = N'1'
  689.          if (@BlankIndex <> 0)
  690.             SELECT @command = N'""' + @Data + N'\Binn\' + N'xpadsi.exe' +  N'"" ' + @InstanceName + N' ' + @nAction +  N' 1 ' + @nStartup
  691.          else
  692.             SELECT @command = @Data + N'\Binn\' + N'xpadsi.exe ' + @InstanceName + N' ' + @nAction +  N' 1 ' + @nStartup
  693.  
  694.          EXECUTE @retval = master.dbo.xp_cmdshell @command
  695.          if (@retval = 0)
  696.          begin
  697.             /* we successfully delete the SCP and all its children,  let's remove the registry keys/values for them */
  698.             if (@nAction = N'3')
  699.             begin
  700.                 EXECUTE @retval = master.dbo.xp_MSADSIReg @InstanceName, @nAction, @ServerName
  701.                 if (@retval <> 0)
  702.                 begin
  703.                     raiserror(14303, -1, -1, N'sp_ActiveDirectory_SCP')
  704.                     return 1
  705.                 end
  706.             end
  707.             /* Get in only if caller asked for create with DB objects */
  708.             if (UPPER(@Action) like N'CREATE_WITH%') or (UPPER(@Action) like N'CREATE_NOU%')
  709.             begin
  710.                /* After we created the SCP object, we create all the database objects */
  711.  
  712.                /* Note that for performance reason, we want to create all the registry entries in one connection */
  713.                EXECUTE @retval = master.dbo.xp_MSADSIObjRegDB @InstanceName, @ServerName
  714.  
  715.                if (UPPER(@Action) like N'CREATE_WITH%')
  716.                begin
  717.                   declare hC cursor for select name from master.dbo.sysdatabases
  718.                end else begin
  719.                   declare hC cursor for select * from msdb.dbo.ADSINewDBs
  720.                end
  721.  
  722.                   declare @DBname sysname
  723.                   open hC
  724.                   fetch next from hC into @DBname
  725.  
  726.                   while (@@FETCH_STATUS = 0)
  727.                begin
  728.                   /* Do the AD part, continue even if we got error from one create */
  729.                   if (@BlankIndex <> 0)
  730.                      SELECT @command = N'""' + @Data + N'\Binn\' + N'xpadsi.exe ' + N'" ' + @InstanceName + N' 1 2 ' + N'"' + @DBname + N'""'
  731.                   else
  732.                      SELECT @command = @Data + N'\Binn\' + N'xpadsi.exe ' + @InstanceName + N' 1 2 ' + N'""' + @DBname + N'""'
  733.                   EXECUTE master.dbo.xp_cmdshell @command
  734.  
  735.                         fetch next from hC into @DBname
  736.                   end
  737.  
  738.                   close hC
  739.                   deallocate hC
  740.  
  741.                /* Get rid of the worker table, which was created by master.dbo.xp_MSADSIObjRegDB */
  742.                drop table msdb.dbo.ADSINewDBs
  743.             end
  744.             return 0
  745.          end else
  746.          begin
  747.             if @retval = @SQLADSI_COM_ERROR 
  748.                 RAISERROR(14350, -1, -1)
  749.             else if @retval = @SQLADSI_UNEXP_ERROR 
  750.                 RAISERROR(14351, -1, -1)
  751.             else if @retval = @SQLADSI_SCP_NOT_FOUND 
  752.                 RAISERROR(14352, -1, -1)
  753.             else if @retval = @SQLADSI_SVC_ACCT_ERROR 
  754.                 RAISERROR(14353, -1, -1)
  755.             else if @retval = @SQLADSI_CANNOT_START_HLP 
  756.                 RAISERROR(14354, -1, -1)
  757.             else if @retval = @SQLADSI_TIMEOUT_WAIT_HLP 
  758.                 RAISERROR(14355, -1, -1)
  759.             else if @retval = @SQLADSI_AD_NOT_INSTALLED 
  760.                 RAISERROR(14356, -1, -1)
  761.             else if @retval = @SQLADSI_PROXY_ACCT_ERROR 
  762.                 RAISERROR(14358, -1, -1)
  763.             /* Failed */
  764.             return 1
  765.          end
  766.       end else
  767.       begin
  768.          raiserror(14303, -1, -1, N'sp_ActiveDirectory_SCP')
  769.          return 1
  770.       end
  771.    end else
  772.    begin
  773.       raiserror(14359, -1, -1)
  774.       return 1
  775.    end
  776. end
  777. go
  778.  
  779. grant execute on sp_ActiveDirectory_SCP to public
  780. go
  781.  
  782. --------------------------------------------------------------------------------
  783. -- END XPSTAR SECTION
  784. --------------------------------------------------------------------------------
  785.  
  786. --------------------------------------------------------------------------------
  787. -- AGENT stored procedures added after release into instmsdb.sql file
  788. --
  789. -- sp_sqlagent_has_server_access
  790. -- sp_set_sqlagent_properties
  791. -- sp_verify_subsystem
  792. --------------------------------------------------------------------------------
  793.  
  794. use msdb
  795. go
  796.  
  797. /**************************************************************/
  798. /* SP_SQLAGENT_HAS_SERVER_ACCESS                              */
  799. /**************************************************************/
  800.  
  801. PRINT ''
  802. PRINT 'Creating procedure sp_sqlagent_has_server_access...'
  803. go
  804. IF (EXISTS (SELECT *
  805.             FROM msdb.dbo.sysobjects
  806.             WHERE (name = 'sp_sqlagent_has_server_access')
  807.               AND (type = 'P')))
  808.   DROP PROCEDURE sp_sqlagent_has_server_access
  809. go
  810. CREATE PROCEDURE sp_sqlagent_has_server_access
  811.   @login_name         sysname = NULL,
  812.   @is_sysadmin_member INT     = NULL OUTPUT
  813. AS
  814. BEGIN
  815.   DECLARE @has_server_access BIT
  816.   DECLARE @is_sysadmin       BIT
  817.   DECLARE @actual_login_name sysname
  818.   DECLARE @cachedate         DATETIME
  819.  
  820.   SET NOCOUNT ON
  821.  
  822.   SELECT @cachedate = NULL
  823.  
  824.   -- remove expired entries from the cache
  825.   DELETE msdb.dbo.syscachedcredentials
  826.   WHERE  DATEDIFF(MINUTE, cachedate, GETDATE()) >= 29
  827.  
  828.   -- query the cache
  829.   SELECT  @is_sysadmin = is_sysadmin_member,
  830.           @has_server_access = has_server_access,
  831.           @cachedate = cachedate
  832.   FROM    msdb.dbo.syscachedcredentials
  833.   WHERE   login_name = @login_name
  834.   AND     DATEDIFF(MINUTE, cachedate, GETDATE()) < 29
  835.  
  836.   IF (@cachedate IS NOT NULL)
  837.   BEGIN
  838.     -- no output variable
  839.     IF (@is_sysadmin_member IS NULL)
  840.     BEGIN
  841.       -- Return result row
  842.       SELECT has_server_access = @has_server_access,
  843.              is_sysadmin       = @is_sysadmin,
  844.              actual_login_name = @login_name
  845.       RETURN
  846.     END
  847.     ELSE
  848.     BEGIN
  849.       SELECT @is_sysadmin_member = @is_sysadmin
  850.       RETURN
  851.     END
  852.   END -- select from cache
  853.  
  854.   CREATE TABLE #xp_results
  855.   (
  856.   account_name      sysname      COLLATE database_default NOT NULL PRIMARY KEY,
  857.   type              NVARCHAR(10) COLLATE database_default NOT NULL,
  858.   privilege         NVARCHAR(10) COLLATE database_default NOT NULL,
  859.   mapped_login_name sysname      COLLATE database_default NOT NULL,
  860.   permission_path   sysname      COLLATE database_default NULL
  861.   )
  862.  
  863.   -- Set defaults
  864.   SELECT @has_server_access = 0
  865.   SELECT @is_sysadmin = 0
  866.   SELECT @actual_login_name = FORMATMESSAGE(14205)
  867.  
  868.   IF (@login_name IS NULL)
  869.   BEGIN
  870.     SELECT has_server_access = 1,
  871.            is_sysadmin       = IS_SRVROLEMEMBER(N'sysadmin'),
  872.            actual_login_name = SUSER_SNAME()
  873.     RETURN
  874.   END
  875.  
  876.   IF (@login_name LIKE '%\%')
  877.   BEGIN
  878.     -- Handle the LocalSystem account ('NT AUTHORITY\SYSTEM') as a special case
  879.     IF (UPPER(@login_name) = N'NT AUTHORITY\SYSTEM')
  880.     BEGIN
  881.       IF (EXISTS (SELECT *
  882.                   FROM master.dbo.syslogins
  883.                   WHERE (UPPER(loginname) = N'BUILTIN\ADMINISTRATORS')))
  884.       BEGIN
  885.         SELECT @has_server_access = hasaccess,
  886.                @is_sysadmin = sysadmin,
  887.                @actual_login_name = loginname
  888.         FROM master.dbo.syslogins
  889.         WHERE (UPPER(loginname) = N'BUILTIN\ADMINISTRATORS')
  890.       END
  891.     END
  892.     ELSE
  893.     BEGIN
  894.       -- Check if the NT login has been explicitly denied access
  895.       IF (EXISTS (SELECT *
  896.                   FROM master.dbo.syslogins
  897.                   WHERE (loginname = @login_name)
  898.                     AND (denylogin = 1)))
  899.       BEGIN
  900.         SELECT @has_server_access = 0,
  901.                @is_sysadmin = sysadmin,
  902.                @actual_login_name = loginname
  903.         FROM master.dbo.syslogins
  904.         WHERE (loginname = @login_name)
  905.       END
  906.       ELSE
  907.       BEGIN
  908.         -- Call xp_logininfo to determine server access
  909.         INSERT INTO #xp_results
  910.         EXECUTE master.dbo.xp_logininfo @login_name
  911.  
  912.         SELECT @has_server_access = CASE COUNT(*)
  913.                                       WHEN 0 THEN 0
  914.                                       ELSE 1
  915.                                     END
  916.         FROM #xp_results
  917.         SELECT @actual_login_name = mapped_login_name,
  918.                @is_sysadmin = CASE UPPER(privilege)
  919.                                 WHEN 'ADMIN' THEN 1
  920.                                 ELSE 0
  921.                              END
  922.         FROM #xp_results
  923.       END
  924.     END
  925.   END
  926.   ELSE
  927.   BEGIN
  928.     -- Standard login
  929.     IF (EXISTS (SELECT *
  930.                 FROM master.dbo.syslogins
  931.                 WHERE (loginname = @login_name)))
  932.     BEGIN
  933.       SELECT @has_server_access = hasaccess,
  934.              @is_sysadmin = sysadmin,
  935.              @actual_login_name = loginname
  936.       FROM master.dbo.syslogins
  937.       WHERE (loginname = @login_name)
  938.     END
  939.   END
  940.  
  941.   -- update the cache only if something is found
  942.   IF  (UPPER(@actual_login_name) <> '(UNKNOWN)')
  943.   BEGIN
  944.     BEGIN TRAN
  945.     IF EXISTS (SELECT * FROM msdb.dbo.syscachedcredentials WITH (TABLOCKX) WHERE login_name = @login_name)
  946.     BEGIN
  947.       UPDATE msdb.dbo.syscachedcredentials
  948.       SET    has_server_access = @has_server_access,
  949.              is_sysadmin_member = @is_sysadmin,
  950.              cachedate = GETDATE()
  951.       WHERE  login_name = @login_name
  952.     END
  953.     ELSE
  954.     BEGIN
  955.       INSERT INTO msdb.dbo.syscachedcredentials(login_name, has_server_access, is_sysadmin_member) 
  956.       VALUES(@login_name, @has_server_access, @is_sysadmin)
  957.     END
  958.     COMMIT TRAN
  959.   END
  960.  
  961.   IF (@is_sysadmin_member IS NULL)
  962.     -- Return result row
  963.     SELECT has_server_access = @has_server_access,
  964.            is_sysadmin       = @is_sysadmin,
  965.            actual_login_name = @actual_login_name
  966.   ELSE
  967.     -- output variable only
  968.     SELECT @is_sysadmin_member = @is_sysadmin
  969. END
  970. go
  971.  
  972.  
  973. /**************************************************************/
  974. /* sp_verify_subsystem                                        */
  975. /**************************************************************/
  976. PRINT N''
  977. PRINT N'Creating procedure sp_verify_subsystem...'
  978. go
  979.  
  980. IF (EXISTS (SELECT * FROM msdb.dbo.sysobjects WHERE (name = N'sp_verify_subsystem') AND (type = 'P')))
  981.   DROP PROCEDURE dbo.sp_verify_subsystem
  982. go
  983.  
  984. CREATE PROCEDURE dbo.sp_verify_subsystem
  985.   @subsystem NVARCHAR(40)
  986. AS
  987. BEGIN
  988.   SET NOCOUNT ON
  989.  
  990.   -- Remove any leading/trailing spaces from parameters
  991.   SELECT @subsystem = LTRIM(RTRIM(@subsystem))
  992.  
  993.   -- NOTE: We don't use the results of sp_enum_sqlagent_subsystems for performance reasons
  994.   IF (UPPER(@subsystem collate SQL_Latin1_General_CP1_CS_AS) IN 
  995.                            (N'ACTIVESCRIPTING',
  996.                             N'CMDEXEC',
  997.                             N'DISTRIBUTION',
  998.                             N'SNAPSHOT',
  999.                             N'LOGREADER',
  1000.                             N'MERGE',
  1001.                             N'TSQL',
  1002.                             N'QUEUEREADER'))
  1003.     RETURN(0) -- Success
  1004.   ELSE
  1005.   BEGIN
  1006.     RAISERROR(14234, -1, -1, '@subsystem', 'sp_enum_sqlagent_subsystems')
  1007.     RETURN(1) -- Failure
  1008.   END
  1009. END
  1010. go
  1011.  
  1012. /**************************************************************/
  1013. /* SP_GET_SQLAGENT_PROPERTIES                                 */
  1014. /**************************************************************/
  1015.  
  1016. PRINT ''
  1017. PRINT 'Creating procedure sp_get_sqlagent_properties...'
  1018. go
  1019. IF (EXISTS (SELECT *
  1020.             FROM msdb.dbo.sysobjects
  1021.             WHERE (name = N'sp_get_sqlagent_properties')
  1022.               AND (type = 'P')))
  1023.   DROP PROCEDURE sp_get_sqlagent_properties
  1024. go
  1025. CREATE PROCEDURE sp_get_sqlagent_properties
  1026. AS
  1027. BEGIN
  1028.   DECLARE @auto_start                  INT
  1029.   DECLARE @startup_account             NVARCHAR(100)
  1030.   DECLARE @msx_server_name             NVARCHAR(30)
  1031.  
  1032.   -- Non-SQLDMO exposed properties
  1033.   DECLARE @sqlserver_restart           INT
  1034.   DECLARE @jobhistory_max_rows         INT
  1035.   DECLARE @jobhistory_max_rows_per_job INT
  1036.   DECLARE @errorlog_file               NVARCHAR(255)
  1037.   DECLARE @errorlogging_level          INT
  1038.   DECLARE @error_recipient             NVARCHAR(30)
  1039.   DECLARE @monitor_autostart           INT
  1040.   DECLARE @local_host_server           NVARCHAR(30)
  1041.   DECLARE @job_shutdown_timeout        INT
  1042.   DECLARE @cmdexec_account             VARBINARY(64)
  1043.   DECLARE @regular_connections         INT
  1044.   DECLARE @host_login_name             sysname
  1045.   DECLARE @host_login_password         VARBINARY(512)
  1046.   DECLARE @login_timeout               INT
  1047.   DECLARE @idle_cpu_percent            INT
  1048.   DECLARE @idle_cpu_duration           INT
  1049.   DECLARE @oem_errorlog                INT
  1050.   DECLARE @sysadmin_only               INT
  1051.   DECLARE @email_profile               NVARCHAR(64)
  1052.   DECLARE @email_save_in_sent_folder   INT
  1053.   DECLARE @cpu_poller_enabled          INT
  1054.  
  1055.   SET NOCOUNT ON
  1056.  
  1057.   -- NOTE: We return all SQLServerAgent properties at one go for performance reasons
  1058.  
  1059.   -- Read the values from the registry
  1060.   IF ((PLATFORM() & 0x1) = 0x1) -- NT
  1061.   BEGIN
  1062.     DECLARE @key NVARCHAR(200)
  1063.  
  1064.     SELECT @key = N'SYSTEM\CurrentControlSet\Services\'
  1065.     IF (SERVERPROPERTY('INSTANCENAME') IS NOT NULL)
  1066.       SELECT @key = @key + N'SQLAgent$' + CONVERT (sysname, SERVERPROPERTY('INSTANCENAME'))
  1067.     ELSE
  1068.       SELECT @key = @key + N'SQLServerAgent'
  1069.  
  1070.     EXECUTE master.dbo.xp_regread N'HKEY_LOCAL_MACHINE',
  1071.                                   @key,
  1072.                                   N'Start',
  1073.                                   @auto_start OUTPUT,
  1074.                                   N'no_output'
  1075.     EXECUTE master.dbo.xp_regread N'HKEY_LOCAL_MACHINE',
  1076.                                   @key,
  1077.                                   N'ObjectName',
  1078.                                   @startup_account OUTPUT,
  1079.                                   N'no_output'
  1080.   END
  1081.   ELSE
  1082.   BEGIN
  1083.     SELECT @auto_start = 3 -- Manual start
  1084.     SELECT @startup_account = NULL
  1085.   END
  1086.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1087.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1088.                                          N'MSXServerName',
  1089.                                          @msx_server_name OUTPUT,
  1090.                                          N'no_output'
  1091.  
  1092.   -- Non-SQLDMO exposed properties
  1093.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1094.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1095.                                          N'RestartSQLServer',
  1096.                                          @sqlserver_restart OUTPUT,
  1097.                                          N'no_output'
  1098.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1099.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1100.                                          N'JobHistoryMaxRows',
  1101.                                          @jobhistory_max_rows OUTPUT,
  1102.                                          N'no_output'
  1103.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1104.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1105.                                          N'JobHistoryMaxRowsPerJob',
  1106.                                          @jobhistory_max_rows_per_job OUTPUT,
  1107.                                          N'no_output'
  1108.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1109.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1110.                                          N'ErrorLogFile',
  1111.                                          @errorlog_file OUTPUT,
  1112.                                          N'no_output'
  1113.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1114.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1115.                                          N'ErrorLoggingLevel',
  1116.                                          @errorlogging_level OUTPUT,
  1117.                                          N'no_output'
  1118.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1119.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1120.                                          N'ErrorMonitor',
  1121.                                          @error_recipient OUTPUT,
  1122.                                          N'no_output'
  1123.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1124.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1125.                                          N'MonitorAutoStart',
  1126.                                          @monitor_autostart OUTPUT,
  1127.                                          N'no_output'
  1128.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1129.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1130.                                          N'ServerHost',
  1131.                                          @local_host_server OUTPUT,
  1132.                                          N'no_output'
  1133.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1134.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1135.                                          N'JobShutdownTimeout',
  1136.                                          @job_shutdown_timeout OUTPUT,
  1137.                                          N'no_output'
  1138.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1139.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1140.                                          N'CmdExecAccount',
  1141.                                          @cmdexec_account OUTPUT,
  1142.                                          N'no_output'
  1143.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1144.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1145.                                          N'RegularConnections',
  1146.                                          @regular_connections OUTPUT,
  1147.                                          N'no_output'
  1148.   DECLARE @OS int
  1149.   EXECUTE master.dbo.xp_MSplatform @OS OUTPUT
  1150.  
  1151.   IF (@OS = 2)
  1152.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1153.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1154.                                          N'HostLoginID',
  1155.                                          @host_login_name OUTPUT,
  1156.                                          N'no_output'
  1157.   ELSE
  1158.   EXECUTE master.dbo.xp_sqlagent_param   0, 
  1159.                                          N'HostLoginID',
  1160.                                          @host_login_name OUTPUT
  1161.  
  1162.   IF (@OS = 2)
  1163.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1164.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1165.                                          N'HostPassword',
  1166.                                          @host_login_password OUTPUT,
  1167.                                          N'no_output'
  1168.   ELSE
  1169.   EXECUTE master.dbo.xp_sqlagent_param   0, 
  1170.                                          N'HostPassword',
  1171.                                          @host_login_password OUTPUT
  1172.  
  1173.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1174.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1175.                                          N'LoginTimeout',
  1176.                                          @login_timeout OUTPUT,
  1177.                                          N'no_output'
  1178.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1179.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1180.                                          N'IdleCPUPercent',
  1181.                                          @idle_cpu_percent OUTPUT,
  1182.                                          N'no_output'
  1183.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1184.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1185.                                          N'IdleCPUDuration',
  1186.                                          @idle_cpu_duration OUTPUT,
  1187.                                          N'no_output'
  1188.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1189.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1190.                                          N'OemErrorLog',
  1191.                                          @oem_errorlog OUTPUT,
  1192.                                          N'no_output'
  1193.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1194.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1195.                                          N'SysAdminOnly',
  1196.                                          @sysadmin_only OUTPUT,
  1197.                                          N'no_output'
  1198.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1199.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1200.                                          N'EmailProfile',
  1201.                                          @email_profile OUTPUT,
  1202.                                          N'no_output'
  1203.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1204.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1205.                                          N'EmailSaveSent',
  1206.                                          @email_save_in_sent_folder OUTPUT,
  1207.                                          N'no_output'
  1208.   EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1209.                                          N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1210.                                          N'CoreEngineMask',
  1211.                                          @cpu_poller_enabled OUTPUT,
  1212.                                          N'no_output'
  1213.   IF (@cpu_poller_enabled IS NOT NULL)
  1214.     SELECT @cpu_poller_enabled = CASE WHEN (@cpu_poller_enabled & 32) = 32 THEN 0 ELSE 1 END
  1215.  
  1216.   -- Return the values to the client
  1217.   SELECT auto_start = CASE @auto_start
  1218.                         WHEN 2 THEN 1 -- 2 means auto-start
  1219.                         WHEN 3 THEN 0 -- 3 means don't auto-start
  1220.                         ELSE 0        -- Safety net
  1221.                       END,
  1222.          msx_server_name = @msx_server_name,
  1223.          sqlagent_type = (SELECT CASE
  1224.                                     WHEN (COUNT(*) = 0) AND (ISNULL(DATALENGTH(@msx_server_name), 0) = 0) THEN 1 -- Standalone
  1225.                                     WHEN (COUNT(*) = 0) AND (ISNULL(DATALENGTH(@msx_server_name), 0) > 0) THEN 2 -- TSX
  1226.                                     WHEN (COUNT(*) > 0) AND (ISNULL(DATALENGTH(@msx_server_name), 0) = 0) THEN 3 -- MSX
  1227.                                     WHEN (COUNT(*) > 0) AND (ISNULL(DATALENGTH(@msx_server_name), 0) > 0) THEN 0 -- Multi-Level MSX (currently invalid)
  1228.                                     ELSE 0 -- Invalid
  1229.                                   END
  1230.                            FROM msdb.dbo.systargetservers),
  1231.          startup_account = @startup_account,
  1232.  
  1233.          -- Non-SQLDMO exposed properties
  1234.          sqlserver_restart = @sqlserver_restart,
  1235.          jobhistory_max_rows = @jobhistory_max_rows,
  1236.          jobhistory_max_rows_per_job = @jobhistory_max_rows_per_job,
  1237.          errorlog_file = @errorlog_file,
  1238.          errorlogging_level = ISNULL(@errorlogging_level, 7),
  1239.          error_recipient = @error_recipient,
  1240.          monitor_autostart = ISNULL(@monitor_autostart, 0),
  1241.          local_host_server = @local_host_server,
  1242.          job_shutdown_timeout = ISNULL(@job_shutdown_timeout, 15),
  1243.          cmdexec_account = @cmdexec_account,
  1244.          regular_connections = ISNULL(@regular_connections, 0),
  1245.          host_login_name = @host_login_name,
  1246.          host_login_password = @host_login_password,
  1247.          login_timeout = ISNULL(@login_timeout, 30),
  1248.          idle_cpu_percent = ISNULL(@idle_cpu_percent, 10),
  1249.          idle_cpu_duration = ISNULL(@idle_cpu_duration, 600),
  1250.          oem_errorlog = ISNULL(@oem_errorlog, 0),
  1251.          sysadmin_only = ISNULL(@sysadmin_only, 0),
  1252.          email_profile = @email_profile,
  1253.          email_save_in_sent_folder = ISNULL(@email_save_in_sent_folder, 0),
  1254.          cpu_poller_enabled = ISNULL(@cpu_poller_enabled, 0)
  1255. END
  1256. go
  1257.  
  1258. GRANT EXECUTE ON sp_get_sqlagent_properties  TO PUBLIC
  1259. go
  1260.  
  1261. /**************************************************************/
  1262. /* SP_SET_SQLAGENT_PROPERTIES                                 */
  1263. /**************************************************************/
  1264. IF EXISTS (SELECT * FROM msdb.dbo.sysobjects WHERE name = N'sp_set_sqlagent_properties' AND type = 'P')
  1265. BEGIN
  1266.   DROP PROCEDURE dbo.sp_set_sqlagent_properties
  1267. END
  1268. go
  1269.  
  1270. PRINT ''
  1271. PRINT 'Create procedure sp_set_sqlagent_properties...'
  1272. go
  1273.  
  1274. CREATE PROCEDURE dbo.sp_set_sqlagent_properties
  1275.   @auto_start                  INT           = NULL, -- 1 or 0
  1276.   -- Non-SQLDMO exposed properties
  1277.   @sqlserver_restart           INT           = NULL, -- 1 or 0
  1278.   @jobhistory_max_rows         INT           = NULL, -- No maximum = -1, otherwise must be > 1
  1279.   @jobhistory_max_rows_per_job INT           = NULL, -- 1 to @jobhistory_max_rows
  1280.   @errorlog_file               NVARCHAR(255) = NULL, -- Full drive\path\name of errorlog file
  1281.   @errorlogging_level          INT           = NULL, -- 1 = error, 2 = warning, 4 = information
  1282.   @error_recipient             NVARCHAR(30)  = NULL, -- Network address of error popup recipient
  1283.   @monitor_autostart           INT           = NULL, -- 1 or 0
  1284.   @local_host_server           NVARCHAR(30)  = NULL, -- Alias of local host server
  1285.   @job_shutdown_timeout        INT           = NULL, -- 5 to 600 seconds
  1286.   @cmdexec_account             VARBINARY(64) = NULL, -- CmdExec account information
  1287.   @regular_connections         INT           = NULL, -- 1 or 0
  1288.   @host_login_name             sysname       = NULL, -- Login name (if regular_connections = 1)
  1289.   @host_login_password         VARBINARY(512) = NULL, -- Login password (if regular_connections = 1)
  1290.   @login_timeout               INT           = NULL, -- 5 to 45 (seconds)
  1291.   @idle_cpu_percent            INT           = NULL, -- 1 to 100
  1292.   @idle_cpu_duration           INT           = NULL, -- 20 to 86400 seconds
  1293.   @oem_errorlog                INT           = NULL, -- 1 or 0
  1294.   @sysadmin_only               INT           = NULL, -- 1 or 0
  1295.   @email_profile               NVARCHAR(64)  = NULL, -- Email profile name
  1296.   @email_save_in_sent_folder   INT           = NULL, -- 1 or 0
  1297.   @cpu_poller_enabled          INT           = NULL  -- 1 or 0
  1298. AS
  1299. BEGIN
  1300.   -- NOTE: We set all SQLServerAgent properties at one go for performance reasons.
  1301.   -- NOTE: You cannot set the value of the properties msx_server_name, is_msx or
  1302.   --       startup_account - they are all read only.
  1303.  
  1304.   DECLARE @res_valid_range           NVARCHAR(100)
  1305.   DECLARE @existing_core_engine_mask INT
  1306.  
  1307.   SET NOCOUNT ON
  1308.  
  1309.   -- Remove any leading/trailing spaces from parameters
  1310.   SELECT @errorlog_file     = LTRIM(RTRIM(@errorlog_file))
  1311.   SELECT @error_recipient   = LTRIM(RTRIM(@error_recipient))
  1312.   SELECT @local_host_server = LTRIM(RTRIM(@local_host_server))
  1313.   SELECT @host_login_name   = LTRIM(RTRIM(@host_login_name))
  1314.   SELECT @email_profile     = LTRIM(RTRIM(@email_profile))
  1315.  
  1316.   -- Make sure values (if supplied) are good
  1317.   IF (@auto_start IS NOT NULL)
  1318.   BEGIN
  1319.     -- NOTE: When setting the the services start value, 2 == auto-start, 3 == Don't auto-start
  1320.     SELECT @auto_start = CASE @auto_start
  1321.                            WHEN 0 THEN 3
  1322.                            WHEN 1 THEN 2
  1323.                            ELSE 3 -- Assume non auto-start if passed a junk value
  1324.                           END
  1325.   END
  1326.  
  1327.   -- Non-SQLDMO exposed properties
  1328.   IF ((@sqlserver_restart IS NOT NULL) AND (@sqlserver_restart <> 0))
  1329.     SELECT @sqlserver_restart = 1
  1330.  
  1331.   IF (@jobhistory_max_rows IS NOT NULL)
  1332.   BEGIN
  1333.     SELECT @res_valid_range = FORMATMESSAGE(14207)
  1334.     IF ((@jobhistory_max_rows < -1) OR (@jobhistory_max_rows = 0))
  1335.     BEGIN
  1336.       RAISERROR(14266, -1, -1, '@jobhistory_max_rows', @res_valid_range)
  1337.       RETURN(1) -- Failure
  1338.     END
  1339.   END
  1340.   ELSE
  1341.   BEGIN
  1342.     EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1343.                                            N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1344.                                            N'JobHistoryMaxRows',
  1345.                                            @jobhistory_max_rows OUTPUT,
  1346.                                            N'no_output'
  1347.     SELECT @jobhistory_max_rows = ISNULL(@jobhistory_max_rows, -1)
  1348.   END
  1349.  
  1350.   IF (@jobhistory_max_rows_per_job IS NOT NULL)
  1351.   BEGIN
  1352.     IF (@jobhistory_max_rows = -1)
  1353.       SELECT @jobhistory_max_rows_per_job = 0
  1354.     ELSE
  1355.     BEGIN
  1356.       IF ((@jobhistory_max_rows_per_job < 1) OR (@jobhistory_max_rows_per_job > @jobhistory_max_rows))
  1357.       BEGIN
  1358.         SELECT @res_valid_range = N'1..' + CONVERT(NVARCHAR, @jobhistory_max_rows)
  1359.         RAISERROR(14266, -1, -1, '@jobhistory_max_rows', @res_valid_range)
  1360.         RETURN(1) -- Failure
  1361.       END
  1362.     END
  1363.   END
  1364.  
  1365.   IF (@errorlogging_level IS NOT NULL) AND ((@errorlogging_level < 1) OR (@errorlogging_level > 7))
  1366.   BEGIN
  1367.     RAISERROR(14266, -1, -1, '@errorlogging_level', '1..7')
  1368.     RETURN(1) -- Failure
  1369.   END
  1370.  
  1371.   IF (@monitor_autostart IS NOT NULL) AND ((@monitor_autostart < 0) OR (@monitor_autostart > 1))
  1372.   BEGIN
  1373.     RAISERROR(14266, -1, -1, '@monitor_autostart', '0, 1')
  1374.     RETURN(1) -- Failure
  1375.   END
  1376.  
  1377.   IF (@job_shutdown_timeout IS NOT NULL) AND ((@job_shutdown_timeout < 5) OR (@job_shutdown_timeout > 600))
  1378.   BEGIN
  1379.     RAISERROR(14266, -1, -1, '@job_shutdown_timeout', '5..600')
  1380.     RETURN(1) -- Failure
  1381.   END
  1382.  
  1383.   IF (@regular_connections IS NOT NULL) AND ((@regular_connections < 0) OR (@regular_connections > 1))
  1384.   BEGIN
  1385.     RAISERROR(14266, -1, -1, '@regular_connections', '0, 1')
  1386.     RETURN(1) -- Failure
  1387.   END
  1388.  
  1389.   IF (@login_timeout IS NOT NULL) AND ((@login_timeout < 5) OR (@login_timeout > 45))
  1390.   BEGIN
  1391.     RAISERROR(14266, -1, -1, '@login_timeout', '5..45')
  1392.     RETURN(1) -- Failure
  1393.   END
  1394.  
  1395.   IF ((@idle_cpu_percent IS NOT NULL) AND ((@idle_cpu_percent < 1) OR (@idle_cpu_percent > 100)))
  1396.   BEGIN
  1397.     RAISERROR(14266, -1, -1, '@idle_cpu_percent', '10..100')
  1398.     RETURN(1) -- Failure
  1399.   END
  1400.  
  1401.   IF ((@idle_cpu_duration IS NOT NULL) AND ((@idle_cpu_duration < 20) OR (@idle_cpu_duration > 86400)))
  1402.   BEGIN
  1403.     RAISERROR(14266, -1, -1, '@idle_cpu_duration', '20..86400')
  1404.     RETURN(1) -- Failure
  1405.   END
  1406.  
  1407.   IF (@oem_errorlog IS NOT NULL) AND ((@oem_errorlog < 0) OR (@oem_errorlog > 1))
  1408.   BEGIN
  1409.     RAISERROR(14266, -1, -1, '@oem_errorlog', '0, 1')
  1410.     RETURN(1) -- Failure
  1411.   END
  1412.  
  1413.   IF (@sysadmin_only IS NOT NULL) AND ((@sysadmin_only < 0) OR (@sysadmin_only > 1))
  1414.   BEGIN
  1415.     RAISERROR(14266, -1, -1, '@sysadmin_only', '0, 1')
  1416.     RETURN(1) -- Failure
  1417.   END
  1418.  
  1419.   IF (@email_save_in_sent_folder IS NOT NULL) AND ((@email_save_in_sent_folder < 0) OR (@email_save_in_sent_folder > 1))
  1420.   BEGIN
  1421.     RAISERROR(14266, -1, -1, 'email_save_in_sent_folder', '0, 1')
  1422.     RETURN(1) -- Failure
  1423.   END
  1424.  
  1425.   IF (@cpu_poller_enabled IS NOT NULL) AND ((@cpu_poller_enabled < 0) OR (@cpu_poller_enabled > 1))
  1426.   BEGIN
  1427.     RAISERROR(14266, -1, -1, 'cpu_poller_enabled', '0, 1')
  1428.     RETURN(1) -- Failure
  1429.   END
  1430.  
  1431.   -- Write out the values
  1432.   IF (@auto_start IS NOT NULL)
  1433.   BEGIN
  1434.     IF ((PLATFORM() & 0x1) = 0x1) -- NT
  1435.     BEGIN
  1436.       DECLARE @key NVARCHAR(200)
  1437.  
  1438.       SELECT @key = N'SYSTEM\CurrentControlSet\Services\'
  1439.       IF (SERVERPROPERTY('INSTANCENAME') IS NOT NULL)
  1440.         SELECT @key = @key + N'SQLAgent$' + CONVERT (sysname, SERVERPROPERTY('INSTANCENAME'))
  1441.       ELSE
  1442.         SELECT @key = @key + N'SQLServerAgent'
  1443.  
  1444.       EXECUTE master.dbo.xp_regwrite N'HKEY_LOCAL_MACHINE',
  1445.                                      @key,
  1446.                                      N'Start',
  1447.                                      N'REG_DWORD',
  1448.                                      @auto_start
  1449.     END
  1450.     ELSE
  1451.       RAISERROR(14546, 16, 1, '@auto_start')
  1452.   END
  1453.  
  1454.   -- Non-SQLDMO exposed properties
  1455.   IF (@sqlserver_restart IS NOT NULL)
  1456.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1457.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1458.                                             N'RestartSQLServer',
  1459.                                             N'REG_DWORD',
  1460.                                             @sqlserver_restart
  1461.   IF (@jobhistory_max_rows IS NOT NULL)
  1462.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1463.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1464.                                             N'JobHistoryMaxRows',
  1465.                                             N'REG_DWORD',
  1466.                                             @jobhistory_max_rows
  1467.   IF (@jobhistory_max_rows_per_job IS NOT NULL)
  1468.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1469.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1470.                                             N'JobHistoryMaxRowsPerJob',
  1471.                                             N'REG_DWORD',
  1472.                                             @jobhistory_max_rows_per_job
  1473.   IF (@errorlog_file IS NOT NULL)
  1474.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1475.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1476.                                             N'ErrorLogFile',
  1477.                                             N'REG_SZ',
  1478.                                             @errorlog_file
  1479.   IF (@errorlogging_level IS NOT NULL)
  1480.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1481.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1482.                                             N'ErrorLoggingLevel',
  1483.                                             N'REG_DWORD',
  1484.                                             @errorlogging_level
  1485.   IF (@error_recipient IS NOT NULL)
  1486.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1487.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1488.                                             N'ErrorMonitor',
  1489.                                             N'REG_SZ',
  1490.                                             @error_recipient
  1491.   IF (@monitor_autostart IS NOT NULL)
  1492.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1493.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1494.                                             N'MonitorAutoStart',
  1495.                                             N'REG_DWORD',
  1496.                                             @monitor_autostart
  1497.   IF (@local_host_server IS NOT NULL)
  1498.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1499.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1500.                                             N'ServerHost',
  1501.                                             N'REG_SZ',
  1502.                                             @local_host_server
  1503.   IF (@job_shutdown_timeout IS NOT NULL)
  1504.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1505.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1506.                                             N'JobShutdownTimeout',
  1507.                                             N'REG_DWORD',
  1508.                                             @job_shutdown_timeout
  1509.   IF (@cmdexec_account IS NOT NULL)
  1510.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1511.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1512.                                             N'CmdExecAccount',
  1513.                                             N'REG_BINARY',
  1514.                                             @cmdexec_account
  1515.   IF (@regular_connections IS NOT NULL)
  1516.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1517.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1518.                                             N'RegularConnections',
  1519.                                             N'REG_DWORD',
  1520.                                             @regular_connections
  1521.  
  1522.   DECLARE @OS int
  1523.   EXECUTE master.dbo.xp_MSplatform @OS OUTPUT
  1524.  
  1525.   IF (@regular_connections = 0)
  1526.   BEGIN
  1527.     IF (@OS = 2)
  1528.     BEGIN
  1529.       EXECUTE master.dbo.xp_instance_regdeletevalue N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'HostLoginID'
  1530.       EXECUTE master.dbo.xp_instance_regdeletevalue N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'HostPassword'
  1531.     END
  1532.     ELSE
  1533.     BEGIN
  1534.       EXECUTE master.dbo.xp_sqlagent_param    2, N'HostLoginID'
  1535.       EXECUTE master.dbo.xp_sqlagent_param    2, N'HostPassword'
  1536.     END
  1537.   END
  1538.  
  1539.   IF (@host_login_name IS NOT NULL)
  1540.   BEGIN
  1541.     IF (@OS = 2)
  1542.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1543.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1544.                                             N'HostLoginID',
  1545.                                             N'REG_SZ',
  1546.                                             @host_login_name
  1547.     ELSE
  1548.     EXECUTE master.dbo.xp_sqlagent_param    1,
  1549.                                             N'HostLoginID',
  1550.                                             @host_login_name
  1551.   END
  1552.  
  1553.   IF (@host_login_password IS NOT NULL)
  1554.   BEGIN
  1555.     IF (@OS = 2)
  1556.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1557.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1558.                                             N'HostPassword',
  1559.                                             N'REG_BINARY',
  1560.                                             @host_login_password
  1561.     ELSE
  1562.     EXECUTE master.dbo.xp_sqlagent_param    1,
  1563.                                             N'HostPassword',
  1564.                                             @host_login_password
  1565.   END
  1566.  
  1567.   IF (@login_timeout IS NOT NULL)
  1568.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1569.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1570.                                             N'LoginTimeout',
  1571.                                             N'REG_DWORD',
  1572.                                             @login_timeout
  1573.   IF (@idle_cpu_percent IS NOT NULL)
  1574.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1575.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1576.                                             N'IdleCPUPercent',
  1577.                                             N'REG_DWORD',
  1578.                                             @idle_cpu_percent
  1579.   IF (@idle_cpu_duration IS NOT NULL)
  1580.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1581.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1582.                                             N'IdleCPUDuration',
  1583.                                             N'REG_DWORD',
  1584.                                             @idle_cpu_duration
  1585.   IF (@oem_errorlog IS NOT NULL)
  1586.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1587.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1588.                                             N'OemErrorLog',
  1589.                                             N'REG_DWORD',
  1590.                                             @oem_errorlog
  1591.   IF (@sysadmin_only IS NOT NULL)
  1592.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1593.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1594.                                             N'SysAdminOnly',
  1595.                                             N'REG_DWORD',
  1596.                                             @sysadmin_only
  1597.   IF (@email_profile IS NOT NULL)
  1598.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1599.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1600.                                             N'EmailProfile',
  1601.                                             N'REG_SZ',
  1602.                                             @email_profile
  1603.   IF (@email_save_in_sent_folder IS NOT NULL)
  1604.     EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1605.                                             N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1606.                                             N'EmailSaveSent',
  1607.                                             N'REG_DWORD',
  1608.                                             @email_save_in_sent_folder
  1609.   IF (@cpu_poller_enabled IS NOT NULL)
  1610.   BEGIN
  1611.     EXECUTE master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
  1612.                                            N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1613.                                            N'CoreEngineMask',
  1614.                                            @existing_core_engine_mask OUTPUT,
  1615.                                            N'no_output'
  1616.     IF ((@existing_core_engine_mask IS NOT NULL) OR (@cpu_poller_enabled = 1))
  1617.     BEGIN
  1618.       IF (@cpu_poller_enabled = 1)
  1619.         SELECT @cpu_poller_enabled = (ISNULL(@existing_core_engine_mask, 0) & ~32)
  1620.       ELSE
  1621.         SELECT @cpu_poller_enabled = (ISNULL(@existing_core_engine_mask, 0) | 32)
  1622.  
  1623.       IF ((@existing_core_engine_mask IS NOT NULL) AND (@cpu_poller_enabled = 32))
  1624.         EXECUTE master.dbo.xp_instance_regdeletevalue N'HKEY_LOCAL_MACHINE',
  1625.                                                       N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1626.                                                       N'CoreEngineMask'
  1627.       ELSE
  1628.         EXECUTE master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
  1629.                                                 N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent',
  1630.                                                 N'CoreEngineMask',
  1631.                                                 N'REG_DWORD',
  1632.                                                 @cpu_poller_enabled
  1633.     END
  1634.   END
  1635.  
  1636.   RETURN(0) -- Success
  1637. END
  1638. go
  1639.  
  1640. --------------------------------------------------------------------------------
  1641. -- END AGENT SECTION
  1642. --------------------------------------------------------------------------------
  1643.  
  1644. --------------------------------------------------------------------------------
  1645. -- END OF FILE: Turn off marking of system objects.
  1646. -- DO NOT ADD ANYTHING AFTER THIS POINT
  1647. --------------------------------------------------------------------------------
  1648. print N''
  1649. go
  1650.  
  1651. exec sp_MS_upd_sysobj_category 2
  1652. go
  1653.  
  1654. exec sp_configure 'allow updates',0
  1655. go
  1656.  
  1657. reconfigure with override
  1658. go
  1659.  
  1660. PRINT N''
  1661. PRINT N'Done updating database objects'
  1662. PRINT N'Finished at ' + convert(nvarchar(25), getdate())
  1663. PRINT N''
  1664. go
  1665.